﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ikvmTools
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FormBorderStyle = FormBorderStyle.FixedSingle;
            text_OutPath.Text = Environment.CurrentDirectory;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
                textBox1.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            }
            else
            {
                e.Effect = DragDropEffects.None;
                textBox1.Text = "";
            }
        }

        private void Form1_DragLeave(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            Array array = ((System.Array)e.Data.GetData(DataFormats.FileDrop));
            foreach (var arr in array)
            {
                lvw_jarpackages.Items.Add(arr.ToString());
            }
        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
                textBox1.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
            textBox1.Text = "";
        }

        private void listView1_DragLeave(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void 删除DToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Deletelvw_packages();
        }

        void Deletelvw_packages()
        {
            for (int i = lvw_jarpackages.SelectedItems.Count - 1; i >= 0; i--)
            {
                lvw_jarpackages.Items.Remove(lvw_jarpackages.SelectedItems[i]);
            }
        }

        private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lvw_jarpackages.Items.Clear();
        }

        private void btn_Csc_Click(object sender, EventArgs e)
        {
            
            string name = text_DotNetName.Text;
            string filePath = text_OutPath.Text;
            StringBuilder sb = new StringBuilder();
            sb.Append(AppDomain.CurrentDomain.BaseDirectory + "ikvm\\ikvmc -out:");
            sb.Append(filePath + "\\" + name);
            for (int i = 0; i < lvw_jarpackages.Items.Count; i++)
            {
                sb.Append(" "+lvw_jarpackages.Items[i].Text);
            }
            richTextBox1.Clear();
            StartCCMD(sb.ToString());

        }
        void StartCCMD(string message)
        {
            richTextBox1.AppendText("开始编译...\r\n");
            //richTextBox1.AppendText(message + "\r\n");
            //ikvmc -out:xxx.dll xxx.jar xxx.jar


            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口
            p.Start();//启动程序
            //向cmd窗口发送输入信息
            p.StandardInput.WriteLine(message);
            System.Threading.Thread.Sleep(5000);
            p.StandardInput.AutoFlush = true;
            p.StandardInput.WriteLine("exit");
            //向标准输入写入要执行的命令。这里使用&是批处理命令的符号，表示前面一个命令不管是否执行成功都执行后面(exit)命令，如果不执行exit命令，后面调用ReadToEnd()方法会假死
            //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令，后者表示必须前一个命令执行失败才会执行后面的命令

            //获取cmd窗口的输出信息
            string output = p.StandardOutput.ReadToEnd();
            richTextBox1.AppendText("结果:\r\n" + output + "\r\n");
            p.WaitForExit();//等待程序执行完退出进程
            p.Close();

        }
    }
}
